<?php
/**
 * @package		简易CMS
 * @subpackage	插件函数包
 * @version		$Id: option_diy.php 19 2012-06-07 17:36:32Z htmambo@gmail.com $
 * @author		Hoping
 * @copyright	Copyright (C) 2010 Hoping Software Studio.
 */
if (! defined ( 'IN_JYCMS' )) {
	exit ( 'Access Denied' );
}

define ( 'CURSCRIPT', 'diy' );
define ( 'NOROBOT', TRUE );

class diy_ctrl extends application {
	function __construct() {
		$this->diy_ctrl ();
	}

	function diy_ctrl() {
		global $_G;
		$this->allowaction = array ('index', 'blockclass', 'delete', 'export', 'import' );
		$this->cachelist = array ('portalcategory' );
		parent::__construct ();
		$task = in_array ( $_GET ['task'], array ('index', 'blockclass', 'delete', 'export', 'import' ) ) ? $_GET ['task'] : 'index';
		$_G ['gp_task'] = $task;
		require_once libfile ( 'function/portal' );
	}
	function onBlockClass() {
		global $_G;
		loadcache ( 'blockclass' );
		if (! $_G ['cache'] ['blockclass']) {
			include_once libfile ( 'function/block' );
			$_G ['cache'] ['blockclass'] = blockclass_cache ();
		}
		include_once template ( "common/diy" );
	}
	function onExport() {
		global $_G;
		if (submitcheck ( 'exportsubmit' )) {
			$tpl = $_POST ['tpl'];
			$frame = $_POST ['frame'];
			$type = $_POST ['type'];
			if (! empty ( $tpl )) {
				tpl_checkperm ( $tpl );
				list ( $tpl, $id ) = explode ( ':', $tpl );
				$tplname = $id ? $tpl . '_' . $id : $tpl;
				$diydata = DB::fetch_first ( "SELECT * FROM #__common_diy_data WHERE targettplname='$tplname'" );
				if (empty ( $diydata ) && $id)
					$diydata = DB::fetch_first ( "SELECT * FROM #__common_diy_data WHERE targettplname='$tpl'" );
				if ($diydata) {
					$filename = $diydata ['targettplname'];
					$diycontent = unserialize ( $diydata ['diycontent'] );
					if (empty ( $diycontent ))
						showmessage ( 'No data can be exported.' );
					if ($frame) {
						$area = '';
						$filename = $frame;
						$framedata = array ();
						foreach ( $diycontent ['layoutdata'] as $key => $value ) {
							$framedata = getobjbyname ( $frame, $value );
							if ($framedata) {
								$area = $key;
								getframeblock ( array ($framedata ['type'] . '`' . $frame => $framedata ['content'] ) );
								break;
							}
						}
					} else {
						foreach ( $diycontent ['layoutdata'] as $key => $value ) {
							if (! empty ( $value ))
								getframeblock ( $value );
						}
					}
					$diycontent ['blockdata'] = block_export ( $_G ['curtplbid'] );
					if ($frame) {
						$diycontent ['spacecss'] = getcssdata ( $diycontent ['spacecss'] );
						$diycontent ['layoutdata'] = array ();
						$area = empty ( $area ) ? 'diy1' : $area;
						$diycontent ['layoutdata'] [$area] [$framedata ['type'] . '`' . $frame] = $framedata ['content'] ? $framedata ['content'] : array ();
					}
					dheader ( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
					dheader ( 'Last-Modified: ' . gmdate ( 'D, d M Y H:i:s' ) . ' GMT' );
					dheader ( 'Cache-Control: no-cache, must-revalidate' );
					dheader ( 'Pragma: no-cache' );
					dheader ( 'Content-Encoding: none' );
					if ($type == 'txt') {
						$str = serialize ( $diycontent );
						dheader ( 'Content-Length: ' . strlen ( $str ) );
						dheader ( 'Content-Disposition: attachment; filename=' . $filename . '.txt' );
						dheader ( 'Content-Type: text/plant' );
					} else {
						require_once libfile ( 'class/xml' );
						$str = array2xml ( $diycontent, true );
						dheader ( 'Content-Length: ' . strlen ( $str ) );
						dheader ( 'Content-Disposition: attachment; filename=' . $filename . '.xml' );
						dheader ( 'Content-Type: text/xml' );
					}
					echo $str;
					exit ();
				}
			}
		}
		showmessage ( 'Wrong operation!', '/' );
	}
	function onImport() {
		global $_G;
		$tpl = $_POST ['tpl'] ? $_POST ['tpl'] : $_GET ['tpl'];
		tpl_checkperm ( $tpl );
		if (submitcheck ( 'importsubmit' )) {
			require_once libfile ( 'class/upload' );
			$upload = new cmsUpload ();
			$upload->init ( $_FILES ['importfile'], 'temp' );
			$attach = $upload->attach;
			if (! $upload->error ()) {
				$upload->save ();
			}
			if ($upload->error ()) {
				showmessage ( $upload->error (), 'index.php', array ('status' => $upload->error () ) );
			} else {
				$arr = import_diy ( $attach ['target'] );
				if (! empty ( $arr )) {
					$search = array ('<script', '</script>', "{", "}", "\r", "\n" );
					$replace = array ('[script', '[/script]', '\{', '\}', '', '' );
					$arr ['css'] = addslashes ( $arr ['css'] );
					$arr ['css'] = str_replace ( array ("{", "}", "\r", "\n" ), array ('\{', '\}', '' ), $arr ['css'] );
					$jsarr = array ('status' => 1, 'css' => $arr ['css'], 'bids' => implode ( ',', $arr ['mapping'] ) );
					foreach ( $arr ['html'] as $key => $value ) {
						$value = addslashes ( $value );
						$value = str_replace ( $search, $replace, $value );
						$value = preg_replace ( '/(\[script [^>]*?)(src=)(.*?\[\/script\])/', '$1[src=]$3', $value );
						$jsarr ['html'] [$key] = $value;
					}
					showmessage ( 'The operation completed!', 'index.php', $jsarr );
				} else {
					showmessage ( 'The operation completed!', 'index.php', array ('status' => 0 ) );
				}
			}
		}
		include_once template ( "common/diy" );
	}
	function onIndex() {
		global $_G, $category, $navtitle;
		$op = in_array ( $_GET ['op'], array ('style', 'diy', 'image', 'export', 'import', 'blockclass' ) ) ? $_GET ['op'] : '';
		if (submitcheck ( 'diysubmit' )) {

			require_once libfile ( 'function/portalcp' );

			$template = getstr ( $_POST ['template'], 50 );
			$savemod = getstr ( $_POST ['savemod'], 1 );
			$recover = getstr ( $_POST ['recover'], 1 );
			$optype = getstr ( $_POST ['optype'], 10 );

			tpl_checkperm ( $template );

			list ( $template, $clonefile ) = explode ( ':', $template );
			list ( $mod, $file ) = explode ( '/', $template );
			$targettplname = $template;

			if ($savemod == '1' && ! empty ( $clonefile )) {
				$targettplname = $template . '_' . $clonefile;
			}

			if ($optype == 'canceldiy') {
				@unlink ( ROOT_PATH . './data/diy/' . $targettplname . '_diy_preview.htm' );
				if ($targettplname == $template)
					@unlink ( ROOT_PATH . './data/diy/' . $targettplname . '_' . $clonefile . '_diy_preview.htm' );
				showmessage ( 'The operation completed!' );
			}
			$iscategory = false;
			if ($template == 'portal/list') {
				$template = getportalcategorytplname ( $clonefile );
				$iscategory = true;
			}

			$primaltplname = checkprimaltpl ( $template );

			if ($recover == '1') {
				$file = './data/diy/' . $targettplname . '.htm';
				if (is_file ( $file . '.bak' )) {
					copy ( $file . '.bak', $file );
				} else {
					showmessage ( 'Backup file does not exist.' );
				}
			} else {
				$templatedata = array ();
				checksecurity ( $_POST ['spacecss'] );
				$_POST ['spacecss'] = dstripslashes ( $_POST ['spacecss'] );
				$templatedata ['spacecss'] = preg_replace ( "/(\<|\>)/is", '', $_POST ['spacecss'] );

				$layoutdata = getstr ( $_POST ['layoutdata'], 0, 1, 0, 0, 1 );
				require_once libfile ( 'class/xml' );
				$templatedata ['layoutdata'] = xml2array ( $layoutdata );
				if (empty ( $templatedata ['layoutdata'] ))
					showmessage ( 'Data format error.' );

				$r = save_diy_data ( $template, $targettplname, $templatedata, true, $optype );

				if ($r && $optype != 'savecache') {
					if (! $iscategory && empty ( $savemod ) && ! empty ( $clonefile )) {
						$delfile = ROOT_PATH . './data/diy/' . $template . '_' . $clonefile . '.htm';
						if (file_exists ( $delfile )) {
							unlink ( $delfile );
							DB::delete ( 'common_template_block', array ('targettplname' => "{$template}_{$clonefile}" ) );
							DB::delete ( 'common_diy_data', array ('targettplname' => "{$template}_{$clonefile}" ) );
							include_once libfile ( 'function/cache' );
							updatecache ( 'diytemplatename' );
						}
					}
				}
			}

			$tourl = empty ( $_POST ['gobackurl'] ) || strpos ( $_POST ['gobackurl'], 'op=add' ) != false || strpos ( $_POST ['gobackurl'], '&diy=yes' ) != false ? str_replace ( '&diy=yes', '', $_SERVER ['HTTP_REFERER'] ) : $_POST ['gobackurl'];

			$tourl = preg_replace ( '/[\?|&]preview=yes/', '', $tourl );

			showmessage ( 'The operation completed!', $tourl, array ('rejs' => $_POST ['rejs'] ) );
		}
		include_once template ( "common/diy" );
	}
}
